#!/bin/bash
clear
if [[ ! -n $(yum list installed | grep nodejs) ]];
then
echo "-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~"
echo
echo "当前服务器尚未安装NodeJs,回车开始安装(仅支持centos)"
echo
echo "-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~"
read
echo
echo "安装过程中不要进行操作"
sleep 3
yum install -y curl
curl -sL https://rpm.nodesource.com/setup | bash -
yum install -y nodejs
clear
echo "-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~"
echo
echo "Node js已安装完成！"
echo "-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~"
fi
ps -ef | grep cp1.1.js | grep -v grep | cut -c 9-15 | xargs kill -9 >/dev/null 2>&1
echo "-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~"
echo
echo "请选择运行模式"
echo
echo "     1:后台启动"
echo "     2:前台启动"
echo
echo "-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~"
echo "输入1或者2(回车默认后台启动）:"
read mode
echo ""
if [[ ! -n $mode ]];
then
echo "CProxy将在后台启动"
fi
echo "请输入监听端口(回车默认8080):"
read port
if [[ ! -n $port ]];
then
port=8080;
echo "CProxy将监听${port}端口"
fi
echo ""
echo "请输入验证头(回车默认为cn):"
read header
if [[ ! -n $header ]];
then
echo "CProxy验证头将为cn"
header="cn";
fi
echo ""
if [[ "2" == "$mode" ]];
then
	node --expose-gc ./cp1.1.js $port $header
	exit 0
elif [[ "2" != "$mode" ]];
then
	rm -f /root/cp.log
	nohup node --expose-gc ./cp1.1.js $port $header > ./cp.log 2>&1 &
	sleep 1
	cat ./cp.log
	exit 0
fi
